﻿using System;
using System.Data;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Web;

/// <summary>
/// ViewPage 的摘要说明
/// </summary>
public class OutPage : StaticBase
{
    public OutPage()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    /// <summary>
    /// 是否操作成功
    /// </summary>
    private bool o_sucess = true;
    /// <summary>
    /// 错误信息
    /// </summary>
    private string errorstring = string.Empty;
    /// <summary>
    /// 模板文件名称
    /// </summary>
    private string masterhtml;
    /// <summary>
    /// 数据源 
    /// </summary>
    //private IEnumerable<DataRow> datasheet;
    private DataTable datasheet;
    /// <summary>
    /// 时间戳
    /// </summary>
    private string name;
    /// <summary>
    /// 时间戳
    /// </summary>
    private string type;
    /// <summary>
    /// 模块类别
    /// </summary>
    private FlagsFileName fname;
    /// <summary>
    /// 生成的文件名前缀
    /// </summary>
    private string prefixname;
    /// <summary>
    /// 输出文件路径
    /// </summary>
    private string serverpath;
    /// <summary>
    /// 指定命名文件的标志列（从数据库中的字段）
    /// </summary>
    // string thekey;
    public override bool Osucess
    {
        get { return o_sucess; }
        set { o_sucess = value; }
    }
    public override string Errorstring
    {
        get { return errorstring; }
        set { errorstring = value; }
    }
    /// <summary>
    /// 构造器静态生成对象
    /// </summary>
    /// <param name="tb">需要生成静态文件的数据源</param>
    /// <param name="fn">文件类别枚举</param>
    /// <param name="time">此字段为数据库表中字段，由该字段指定生成的文件名字标志 </param>
    public OutPage(DataTable tb, FlagsFileName fn,string savename, string mold)
    {
        this.name = savename;
        this.type = mold;
        this.fname = fn;
        this.datasheet = tb;
        this.masterhtml = FileName[fn] + ".html";
        WriteFile();
    }
    public OutPage(DataTable tb, FlagsFileName fn, string mold)
    {
        this.type = mold;
        this.fname = fn;
        this.datasheet = tb;
        this.masterhtml = FileName[fn] + ".html";
        WriteFile();
    }

    protected override bool WriteFile()
    {
        string str = "";
        try//从指定模板文件中读取html代码
        {
            sr = new StreamReader(HttpContext.Current.Server.MapPath(PagePath + this.fname + '/' + this.masterhtml), code);
            str = sr.ReadToEnd();
        }
        catch (Exception ex)//异常则指定返回的错误信息 
        {
            sr.Close();
            sr.Dispose();
            this.o_sucess = false;
            this.errorstring = ex.Message;
            return this.o_sucess;
        }
        finally
        {
            sr.Close();
            sr.Dispose();
        }
        //要保存的文件名称
        string filepath = string.Empty;
        string newString = string.Empty;
        string changestring = "";//要更改的字符串
        serverpath = HttpContext.Current.Server.MapPath(SavePath + this.fname + '/');
        //文件夹是否存在
        if (!Directory.Exists(serverpath))
        {
            Directory.CreateDirectory(serverpath);
        }
        
        try
        {
            switch (this.type)
            {
                case "single":
                    newString = str;
                    filepath = serverpath + this.name + ".html";//给文件命名
                    foreach (DataColumn c in this.datasheet.Columns)//遍历单个数据表的列名
                    {
                        changestring = "$" + c.ColumnName + "$";
                        newString = newString.Replace(changestring, this.datasheet.Rows[0][c].ToString());
                    }
                    sw = new StreamWriter(filepath, false, code);
                    Thread.Sleep(150);
                    sw.Write(newString);
                    sw.Flush();
                    break;
                case "batch":
                    prefixname = FileName[fname];//名称前缀
                    foreach (DataRow row in this.datasheet.Rows)
                    {
                        newString = str;
                        filepath = serverpath + prefixname + "_" + DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + ".html";//给文件命名
                        foreach (DataColumn c in row.Table.Columns)//遍历单个数据表的列名
                        {
                            changestring = "$" + c.ColumnName + "$";
                            newString = newString.Replace(changestring, row[c].ToString());
                        }
                        sw = new StreamWriter(filepath, false, code);
                        Thread.Sleep(150);
                        sw.Write(newString);
                        sw.Flush();
                    }
                    break;
                default:break;
            }

        }
        catch (Exception ex)
        {
            this.o_sucess = false;
            this.errorstring = ex.Message;
            return this.o_sucess;
        }
        finally
        {
            sw.Close();
        }


        sw.Dispose();
        return true;
    }


}